package com.ruoyi.dingtalk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.dingtalk.dao.DdUserDao;
import com.ruoyi.dingtalk.domain.DdUser;
import com.ruoyi.dingtalk.mapper.DdUserMapper;
import com.ruoyi.dingtalk.service.DdUserService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class DdUserServiceImpl extends ServiceImpl<DdUserMapper, DdUser> implements DdUserService {
    private DdUserDao ddUserDao;
    private ISysUserService iSysUserService;

    @Autowired
    public void setDdUserDao(DdUserDao ddUserDao) {
        this.ddUserDao = ddUserDao;
    }

    @Autowired
    public void setiSysUserService(ISysUserService iSysUserService) {
        this.iSysUserService = iSysUserService;
    }

    /**
     * 根据钉钉userId获取用户的名称
     *
     * @param ddUserId 钉钉的id
     * @return 用户名称
     */
    @Override
    public String getUserNameByDingUserId(String ddUserId) {
        Optional<DdUser> byUserId = ddUserDao.findByUserId(ddUserId);
        if (!byUserId.isPresent()) {
            return null;
        }
        DdUser ddUser = byUserId.get();
        Long sysUserId = ddUser.getSysUserId();
        if (Objects.isNull(sysUserId)) {
            log.warn("钉钉用户:{},未绑定系统用户id!", ddUser);
            return null;
        }
        return iSysUserService.getUserNameById(sysUserId);
    }


}
